package com.xmy.cultivate.service.impl;

import com.xmy.cultivate.constants.CommonConstant;
import com.xmy.cultivate.entity.RecruitPushLog;
import com.xmy.cultivate.entity.Staff;
import com.xmy.cultivate.respons.QyWeChartPushRs;
import com.xmy.cultivate.service.IMqPushService;
import com.xmy.cultivate.service.IQyWeChartPushService;
import com.xmy.cultivate.service.IRecruitPushLogService;
import com.xmy.cultivate.service.IStaffService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;

@Service
public class QyWeChartPushServiceImpl implements IQyWeChartPushService {

    @Autowired
    IMqPushService iMqPushService;

    @Autowired
    IRecruitPushLogService iRecruitPushLogService;

    @Autowired
    IStaffService iStaffService;

    @Override
    public boolean pushMsgRecruit(String qyWecharId, String title, String content, Long recruitId, Integer pushType, Long pushTecherId){
        //推送内容
        QyWeChartPushRs qyWeChartPushRs = new QyWeChartPushRs();
        qyWeChartPushRs.setToUser(qyWecharId);
        qyWeChartPushRs.setTitle(title);
        qyWeChartPushRs.setContent(content);

        qyWeChartPushRs.setRemark("请及时查看");
        qyWeChartPushRs.setDateTime(LocalDateTime.now().withNano(0).toString().replace("T", " "));
        qyWeChartPushRs.setLinkUrl(CommonConstant.teacherWebUrl);
        //推送到企业微信
        iMqPushService.pushContentForQyWeChar(qyWeChartPushRs);

        RecruitPushLog log = new RecruitPushLog();
        log.setRecruitId(recruitId);
        log.setType(pushType); // 3给主管推送试岗通过或者试岗淘汰
        log.setPushStaffId(pushTecherId);
        log.setStatus(1);  //1已推送
        log.setRemark(content);  //1已推送
        iRecruitPushLogService.save(log);
        return true;
    }

    /**
     * 人事推送给面试官
     * @param title
     * @param content
     * @param recruitId
     * @param pushType
     * @return
     */
    @Override
    public boolean pushMsgRecruitsDunford(String title, String content, Long recruitId, Integer pushType){
        try {
            Staff staff = iStaffService.getById(CommonConstant.dunfordTeacherId);

            //推送内容
            QyWeChartPushRs qyWeChartPushRs = new QyWeChartPushRs();
            qyWeChartPushRs.setToUser(staff.getQyWecharId());
            qyWeChartPushRs.setTitle(title);
            qyWeChartPushRs.setContent(content);

            qyWeChartPushRs.setRemark("请及时查看");
            qyWeChartPushRs.setDateTime(LocalDateTime.now().withNano(0).toString().replace("T", " "));
            qyWeChartPushRs.setLinkUrl(CommonConstant.teacherWebUrl);
            //推送到企业微信
            iMqPushService.pushContentForQyWeChar(qyWeChartPushRs);

            RecruitPushLog log = new RecruitPushLog();
            log.setRecruitId(recruitId);
            log.setType(pushType); // 3给主管推送试岗通过或者试岗淘汰
            log.setPushStaffId(staff.getId());
            log.setStatus(1);  //1已推送
            log.setRemark(content);  //1已推送
            iRecruitPushLogService.save(log);

        }catch (Exception e){

        }
        return true;
    }


    /**
     *
     * @param title
     * @param content
     * @param recruitId
     * @param pushType
     * @return
     */
    @Override
    public boolean push(String qyWecharId, String title, String content, Long recruitId, Integer pushType, Long pushTecherId){
        //推送内容
        QyWeChartPushRs qyWeChartPushRs = new QyWeChartPushRs();
        qyWeChartPushRs.setToUser(qyWecharId);
        qyWeChartPushRs.setTitle(title);
        qyWeChartPushRs.setContent(content);

        qyWeChartPushRs.setRemark("请及时查看");
        qyWeChartPushRs.setDateTime(LocalDateTime.now().withNano(0).toString().replace("T", " "));
        qyWeChartPushRs.setLinkUrl(CommonConstant.teacherWebUrl);
        //推送到企业微信
        iMqPushService.pushContentForQyWeChar(qyWeChartPushRs);

        RecruitPushLog log = new RecruitPushLog();
        log.setRecruitId(recruitId);
        log.setType(pushType);
        log.setPushStaffId(pushTecherId);
        log.setStatus(1);
        log.setRemark(content);
        iRecruitPushLogService.save(log);
        return true;
    }
}
